package com.ldu.houserent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldu.houserent.common.vo.HouseSearchVO;
import com.ldu.houserent.entity.House;
import com.ldu.houserent.mapper.HouseMapper;
import com.ldu.houserent.service.HouseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/*
    房屋服务接口实现类
 */
@Service
public class HouseServiceImpl implements HouseService {

    @Resource
    private HouseMapper houseMapper;      //实例化接口

    //获取mapper对象
    @Override
    public BaseMapper<House> getRepository() {
        return houseMapper;
    }

    //获取查询器
    @Override
    public QueryWrapper<House> getQueryWrapper(House house) {
        QueryWrapper<House> queryWrapper=new QueryWrapper<>();
        if (house!=null && house.getUserId()!=null){    //说明这不是管理员，而是房东进行的查询
            queryWrapper.eq("user_id",house.getUserId());
        }
        return queryWrapper;
    }

    //获取带参数的查询器
    @Override
    public QueryWrapper<House> getQueryWrapper(Map<String, Object> condition) {
        return null;
    }

    //根据用户id和房产证编号查询合租房屋
    @Override
    public List<House> findByUserIdAndCetificateNoAndRentType(Long userId, String cetificateNo, String rentType) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("cetificate_no",cetificateNo);
        queryWrapper.eq("rent_type",rentType);
        return houseMapper.selectList(queryWrapper);        //返回查询的结果
    }

    //根据出租类型获取最新的n条房屋信息
    @Override
    public List<House> findTopList(String rentType, Integer limit) {    //rentType租房类型，limit限制多少条
        return houseMapper.findTopList(rentType,limit);
    }

    //获得房屋分页数据
    @Override
    public Page<House> getHousePage(HouseSearchVO houseSearchVO, Page<House> page) {
        if (houseSearchVO==null){       //如果数据为空
            houseSearchVO = new HouseSearchVO();
        }else {
            //获取到月租金
            String priceRange = houseSearchVO.getPriceRange();
            Integer minPrice=0;
            Integer maxPrice=20000;
            if (StringUtils.isNotEmpty(priceRange)){        //如果priceRange不是空，则将其分解成数组
                String[] arr=priceRange.split(";");     //以分号分隔的两个数的数组
                if (arr.length==2){
                    minPrice=Integer.valueOf(arr[0]);
                    maxPrice=Integer.valueOf(arr[1]);
                }
            }
            houseSearchVO.setMinPrice(minPrice);        //设置最低价
            houseSearchVO.setMaxPrice(maxPrice);        //设置最高价
            //获取到房屋面积
            String areaRange = houseSearchVO.getAreaRange();
            Integer minArea=0;
            Integer maxArea=400;
            if (StringUtils.isNotEmpty(areaRange)){        //如果areaRange不是空，则将其分解成数组
                String[] arr=areaRange.split(";");     //以分号分隔的两个数的数组
                if (arr.length==2){
                    minArea=Integer.valueOf(arr[0]);
                    maxArea=Integer.valueOf(arr[1]);
                }
            }
            houseSearchVO.setMinArea(minArea);
            houseSearchVO.setMaxArea(maxArea);
        }//else
        //查询房屋，返回list
        List<House> list = houseMapper.searchHouse(houseSearchVO,page);
        //将list封装到Page中
        page.setRecords(list);      //设置记录
        return page;
    }
}
